﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Practices.Unity;

namespace Multiprocessing.Core.Units
{
    public sealed class ProcessingBlockFactory : IProcessingBlockFactory
    {
        #region Fields

        private static readonly Lazy<ProcessingBlockFactory> Lazy =
            new Lazy<ProcessingBlockFactory>(() => new ProcessingBlockFactory());

        private readonly IUnityContainer _container;

        #endregion

        #region Properties

        public static ProcessingBlockFactory Instance
        {
            get { return Lazy.Value; }
        }

        #endregion

        #region Constructors

        private ProcessingBlockFactory()
        {
            _container = new UnityContainer();
            _container.RegisterType<TaskProcessingBlock>();
            _container.RegisterType<ThreadProcessingBlock>();
        }

        #endregion

        #region Methods

        public IProcessingBlock CreateProcessingBlock<TProcessingBlock>(int size) 
            where TProcessingBlock : IProcessingBlock
        {
            return _container.Resolve<TProcessingBlock>(new ParameterOverride("size", size));
        }

        public IProcessingBlock CreateProcessingBlock(Type processingBlockType, int size)
        {
            return (IProcessingBlock) _container.Resolve(processingBlockType, new ParameterOverride("size", size));
        }

        #endregion
    }
}
